package montecarlo.algorithms;

import java.util.Random;

public class Single extends AbstractClassMC{

	@Override
	public double monteCarloStandardOption(String callPutFlag, double s,
			double x, double r, double t, double b, double v, int nSteps,
			int nSimulations) throws Exception{
		double dt = 0.0, st = 0.0, sum = 0.0, drift = 0.0, vsqrdt = 0.0, estimatePrice = 0.0;
		int i = 0, j = 0, z = 0;
		dt = t / nSteps;
		drift = (b - Math.pow(v, 2) / 2) * dt;
		vsqrdt = v * Math.sqrt(dt);

		if (callPutFlag.equals("c"))
			z = 1;
		else if (callPutFlag.equals("p"))
			z = -1;
		else
			System.out.println("File to read doesn't have the right format.");

		Random random = new Random();
		for (i = 1; i <= nSimulations; i++) {
			st = s;
			for (j = 1; j <= nSteps; j++) {
				st = st * Math.exp(drift + vsqrdt * random.nextGaussian());
			}
			sum += Math.max(z * (st - x), 0);
		}
		estimatePrice = Math.exp(-r * t) * (sum / nSimulations);
		return estimatePrice;
	}

}
